package com.blue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blue.entity.Line;
import com.blue.entity.LineStation;
import com.blue.entity.Station;
import com.blue.mapper.LineMapper;
import com.blue.mapper.LineStationMapper;
import com.blue.mapper.StationMapper;
import com.blue.service.LineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blue.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.Collections;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blue
 * @since 2024-11-25
 */
@Service
public class LineServiceImpl extends ServiceImpl<LineMapper, Line> implements LineService {
    @Autowired
    LineMapper lineMapper;
    @Autowired
    StationMapper stationMapper;
    @Autowired
    LineStationMapper lineStationMapper;
    @Autowired
    RedisUtils redisUtils;
    @Override
    public Line findLineByName(String name) {
        Line line = null;
        if(redisUtils.hasKey(name)) {
           line = (Line) redisUtils.get(name);}
        else {
            QueryWrapper<Line> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("line_name", name);
            line =  lineMapper.selectOne(objectQueryWrapper);
            redisUtils.set(name, line);
        }
        return line;
    }

    @Override
    public List<Station> findStations(Line line) {
        //获取line id
        int id = line.getLineId();

        List<Station> lists = null;
        /*key : l{id}stations*/
        if(!redisUtils.hasKey(String.valueOf(id))) {
            //获取linestation查询条件对象
            QueryWrapper<LineStation> lineStationQueryWrapper = new QueryWrapper<>();
            //设置查询条件"line_id"=id
            lineStationQueryWrapper.eq("line_id", id);
            //取出符合条件的linestation
            List<LineStation> lineStations = lineStationMapper.selectList(lineStationQueryWrapper);
            //获取station查询条件对象
            QueryWrapper<Station> stationQueryWrapper = new QueryWrapper<>();
            // 从lineStations中提取station_id，并设置查询条件
            if (!lineStations.isEmpty()) {
                stationQueryWrapper.in("station_id", lineStations.stream().map(LineStation::getStationId).collect(Collectors.toList()));
                // 取出符合条件的station
                lists = stationMapper.selectList(stationQueryWrapper);
            } else {
                // 如果没有符合条件的lineStation，返回空列表
                lists =  Collections.emptyList();
            }
            redisUtils.set("l"+String.valueOf(id)+"stations", lists);
        }
        else {
            lists = (List<Station>) redisUtils.get("l"+String.valueOf(id)+"stations");
        }
        return lists;
    }

    @Override
    public Line findLineById(int id) {
        Line line = null;
        /*l{id}*/
        if(redisUtils.hasKey("l"+String.valueOf(id))) {
            line = (Line) redisUtils.get(String.valueOf(id));
        }else {
            line = lineMapper.selectById(id);
            redisUtils.set("l"+String.valueOf(id),line);
        }
        return line;
    }

}

